import 'package:film/controller/auth_controller.dart';
import 'package:film/controller/mine_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class MineScreen extends StatelessWidget {
  final MineController mineController = Get.find<MineController>();
  final AuthController authController = Get.put(AuthController());

  MineScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: SafeArea(
        // 保证内容在安全区域内
        child: SingleChildScrollView(
          // 如果内容可能超出屏幕，则允许滚动
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 20.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 1. 用户信息部分
              _buildUserInfoSection(context),
              const SizedBox(height: 30),
              // 2. 顶部统计数据部分
              Row(
                mainAxisAlignment: MainAxisAlignment.start, // 均匀分布
                children: [
                  _buildTopStatsSection(
                    context,
                    '0',
                    '播放记录',
                    () => null,
                  ),
                  _buildTopStatsSection(
                    context,
                    '0',
                    '浏览历史',
                    () => null,
                  ),
                  _buildTopStatsSection(
                    context,
                    '0',
                    '我的追番',
                    () => null,
                  ),
                ],
              ),
              const SizedBox(height: 30),
              // 3. 操作项网格部分
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  _buildActionItemsRow(
                    context,
                    Icons.diamond,
                    '会员中心',
                    () => authController.checkToken('/vip'),
                  ),
                  _buildActionItemsRow(
                    context,
                    Icons.chat,
                    '留言求片',
                    () => authController.checkToken('/message'),
                  ),
                  _buildActionItemsRow(
                    context,
                    Icons.security,
                    '修改密码',
                    () => authController.checkToken('/updatePassword'),
                  ),
                  _buildActionItemsRow(
                    context,
                    Icons.cached,
                    '清理缓存',
                    mineController.handleActionTap,
                  ),
                ],
              ),
              const SizedBox(height: 200),
              Obx(
                () => authController.loginFlag.value ? _buildCompleteButton() : SizedBox.shrink(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 构建用户信息部分
  Widget _buildUserInfoSection(BuildContext context) {
    return GestureDetector(
      onTap: () => authController.loginFlag.value ? authController.checkToken('/member') : Get.toNamed('/login'),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Row(
          children: [
            Obx(
              () => SizedBox(
                width: 60,
                height: 60,
                child: ClipOval(
                  child: authController.loginFlag.value
                      ? Image.network(
                          authController.userInfo.value.userImg!,
                          fit: BoxFit.cover,
                          errorBuilder: (context, error, stack) => const Icon(
                            Icons.person,
                            size: 50,
                            color: Colors.grey,
                          ),
                        )
                      : Image.asset('assets/images/tx.png'),
                ),
              ),
            ),
            const SizedBox(width: 10),
            Obx(
              () => authController.loginFlag.value
                  ? Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(authController.userInfo.value.nickname ?? ''),
                        Text(
                          '注册时间 ${DateFormat('yyyy-MM-dd').format(DateTime.parse(authController.userInfo.value.createTime!))}',
                          style: const TextStyle(
                            fontSize: 12,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    )
                  : Text(
                      mineController.loginStatusText.value,
                      style: const TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.w600,
                        color: Colors.black87,
                      ),
                    ),
            ),
          ],
        ),
      ),
    );
  }

  // 构建顶部统计数据部分
  Widget _buildTopStatsSection(
    BuildContext context,
    String count,
    String label,
    VoidCallback onTap,
  ) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: 100,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              count,
              style: const TextStyle(
                fontSize: 20,
                color: Colors.black87,
              ),
            ),
            const SizedBox(height: 6),
            Text(
              label,
              style: TextStyle(
                fontSize: 12,
                color: Colors.grey[600],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 构建单行操作项
  Widget _buildActionItemsRow(
    BuildContext context,
    IconData icon,
    String label,
    VoidCallback onTap,
  ) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              icon,
              size: 20,
              color: Colors.grey,
            ),
            const SizedBox(height: 8),
            Text(
              label,
              style: TextStyle(
                fontSize: 12,
                color: Colors.grey[700],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 退出登录
  Widget _buildCompleteButton() {
    return Center(
      child: SizedBox(
        width: 200,
        height: 50,
        child: ElevatedButton(
          onPressed: authController.logout,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.red[400],
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(25),
            ),
            elevation: 0,
          ),
          child: const Text(
            '退出登录',
            style: TextStyle(
              fontSize: 18,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
